#include "Lua.h"
#include "LuaTest.h"
#include "Time.h"

LuaTest::LuaTest( void )
{

}

LuaTest::~LuaTest( void )
{

}
int CF_GetTime(lua_State* L)
{
	if ( !lua_isstring(L, -1) )
		luaL_error(L, "CF_GetTime param error");
	char* pTimeFmt = (char*)lua_tostring(L, -1);
	const char* pTimeStr = GetStrTime(pTimeFmt);
	if ( pTimeStr == NULL )
		luaL_error(L, "CF_GetTime GetStrTime error");
	lua_pushnumber(L, GetTime());
	lua_pushstring(L, pTimeStr);
	return 2;
}
bool LuaTest::Test()
{
	printf("=================LuaTest=================\n");
	Lua lua;
	lua.Init();

	int width = 0;
	int height= 0;
	lua.SetLuaVarInt("width0", 50);
	lua.SetLuaVarInt("height0", 50);
	lua.SetLuaVarTable("tab", "bdisp", true, 10.1, 5, "lele", &lua );
	bool bValue = false;
	double dValue = 0;
	int  nValue = 0;
	char* pChar = NULL;
	void* pVoid = NULL;
	lua.GetLuaVarTable("tab", "bdisp", &bValue, &dValue, &nValue, &pChar, &pVoid);
	printf("%d %f %d, %s, %d\n", bValue, dValue, nValue, pChar, pVoid);
	lua.ResetIndex();
	luaL_Reg reg[] = {
		{"CFGetTime", CF_GetTime},
	};
	lua.RegsterCFunc(reg, 1);
	//lua.RegsterCFunc("CFGetTime", CF_GetTime);
	printf("------------test1.lua------------\n");
	if ( !lua.DoLuaFile("Lua/test1.lua") ){
		printf("%s\n", lua.GetLuaError());
		lua.ResetIndex();
	}
	printf("------------test1.lua------------\n\n");

	lua.GetLuaVarInt("width0", width);
	lua.GetLuaVarInt("height0", height);
	printf("width0:%d height0:%d\n", width, height);
	width = height = 0;

	lua.GetLuaVarInt("width1", width);
	lua.GetLuaVarInt("height1", height);
	printf("width1:%d height1:%d\n", width, height);
	width = height = 0;

	printf("------------test2.lua------------\n");
	if ( !lua.DoLuaFile("Lua/test2.lua") ){
		printf("%s\n", lua.GetLuaError());
		lua.ResetIndex();
	}
	printf("------------test2.lua------------\n\n");

	lua.GetLuaVarInt("width0", width);
	lua.GetLuaVarInt("height0", height);
	printf("width0:%d height0:%d\n", width, height);
	width = height = 0;
	lua.GetLuaVarInt("width1", width);
	lua.GetLuaVarInt("height1", height);
	printf("width1:%d height1:%d\n", width, height);
	width = height = 0;
	lua.GetLuaVarInt("width2", width);
	lua.GetLuaVarInt("height2", height);
	printf("width2:%d height2:%d\n", width, height);
	width = height = 0;

	printf("------------LFfunc1------------\n");
	int sum = 0;
	lua.CallLuaFunc("LFfunc1", "ii>i", 1, 2, &sum);
	lua.ResetIndex();
	printf("sum:%d\n", sum);
	printf("------------LFfunc1------------\n\n");
	printf("------------LFfunc2------------\n");
	lua.CallLuaFunc("LFfunc2", "sss>", "123", "456", "789");
	lua.ResetIndex();
	printf("------------LFfunc2------------\n\n");
	printf("------------LFfunc3------------\n");
	if ( !lua.CallLuaFunc("LFfunc3", "") ){
		printf("%s\n", lua.GetLuaError());
	}
	lua.ResetIndex();
	printf("------------LFfunc3------------\n\n");

	printf("=================LuaTest=================\n\n");
	return true;
}
